package com.bj58.groupbuy.action.controllers;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.bj58.groupbuy.action.util.MyServletRequestUtils;

import com.bj58.groupbuy.action.service.DBService;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.util.IOUtils;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.BeatContext;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.GET;
import com.bj58.wf.mvc.annotation.Path;

@Path("dbtools")
public class DBToolsController extends MvcController{

	private DBService dbService = ServiceLocator.get(DBService.class);

	/**主机数据库名列表 - 查询
	 * @return
	 */
	@Path("displaydb")
	@GET
	public ActionResult searchDBNames() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat) {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String hostname = MyServletRequestUtils.getStringParameter(request, "hostname","*");
				try {
					JSONObject json = new JSONObject();
					List<String> dbNames =  new ArrayList<String>();//数据库名称
					String result = "right";
					if(!"*".equals(hostname))
						dbNames = dbService.getDBNames(hostname);
					else
						result = "error";
					json.accumulate("dbNameList", dbNames);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	/**主机数据表名列表 - 查询
	 * @return
	 */
	@Path("displayTable")
	@GET
	public ActionResult searchTableNames() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat) {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String hostname = MyServletRequestUtils.getStringParameter(request, "hostname","*");
				String dbname = MyServletRequestUtils.getStringParameter(request, "dbname","*");
				try {
					JSONObject json = new JSONObject();
					List<String> tableNames =  new ArrayList<String>();//数据表名称
					String result = "right";
					if("*".equals(hostname)||"*".endsWith(dbname))
						result = "error";
					else
						tableNames = dbService.getTableNames(hostname,dbname);
					json.accumulate("tableNameList", tableNames);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
}
